package fp.thetrainline.screens;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.WebElement;

import fp.thetrainline.utils.DriverSupport;
import fp.thetrainline.utils.ElementName;
import fp.thetrainline.utils.ElementType;

public class OutwardTrains {

	public static void pressCheckFaresAndBuyTickets( DriverSupport driver ) {
		driver.click( ElementType.CLASS, ElementName.CHECK_FARES_AND_BUY_TICKETS );
	}
	
	public static String getDepTime( DriverSupport driver ) {
		return getLegTime( driver, ElementName.DEP_TIME );
	}
	
	public static String getDepStation( DriverSupport driver ) {
		return getLegStation( driver, ElementName.DEP_STATION );
	}
	
	public static String getArrStation( DriverSupport driver ) {
		return getLegStation( driver, ElementName.ARR_STATION );
	}
	
	public static String getArrTime( DriverSupport driver ) {
		return getLegTime( driver, ElementName.ARR_TIME );
	}
	
	private static String getLegTime( DriverSupport driver, String elementName ) {
		WebElement time = driver.findElement( ElementType.XPATH, elementName );
		String timeValue = time.getText();
		
		String regex = "\\d*:\\d*";
		Pattern pattern = Pattern.compile( regex );
		Matcher matcher = pattern.matcher( timeValue );
		
		String result = "";
		
		while( matcher.find() ) {
			result = matcher.group();
		}
		
		return result;
	}
	
	private static String getLegStation( DriverSupport driver, String elementName ) {
		WebElement station = driver.findElement( ElementType.XPATH, elementName );
		String stationString = station.getText();
		
		return stationString;
	}
	
}
